package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.McgsJjf2zBo;
import com.ruoyi.tb.lmt.domain.vo.McgsJjf2zVo;
import com.ruoyi.tb.lmt.domain.McgsJjf2z;
import com.ruoyi.tb.lmt.mapper.McgsJjf2zMapper;
import com.ruoyi.tb.lmt.service.IMcgsJjf2zService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 制动阀试验台_紧急阀2组_MCGSService业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class McgsJjf2zServiceImpl implements IMcgsJjf2zService {

    private final McgsJjf2zMapper baseMapper;

    /**
     * 查询制动阀试验台_紧急阀2组_MCGS
     */
    @Override
    public McgsJjf2zVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询制动阀试验台_紧急阀2组_MCGS列表
     */
    @Override
    public TableDataInfo<McgsJjf2zVo> queryPageList(McgsJjf2zBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<McgsJjf2z> lqw = buildQueryWrapper(bo);
        Page<McgsJjf2zVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询制动阀试验台_紧急阀2组_MCGS列表
     */
    @Override
    public List<McgsJjf2zVo> queryList(McgsJjf2zBo bo) {
        LambdaQueryWrapper<McgsJjf2z> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<McgsJjf2z> buildQueryWrapper(McgsJjf2zBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<McgsJjf2z> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, McgsJjf2z::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDjNo()), McgsJjf2z::getDjNo, bo.getDjNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTime()), McgsJjf2z::getMcgsTime, bo.getMcgsTime());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeMs()), McgsJjf2z::getMcgsTimeMs, bo.getMcgsTimeMs());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsCzz()), McgsJjf2z::getMcgsCzz, bo.getMcgsCzz());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJxbh()), McgsJjf2z::getMcgsJxbh, bo.getMcgsJxbh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsFh()), McgsJjf2z::getMcgsFh, bo.getMcgsFh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsSch()), McgsJjf2z::getMcgsSch, bo.getMcgsSch());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf1()), McgsJjf2z::getMcgsJjf1, bo.getMcgsJjf1());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf2()), McgsJjf2z::getMcgsJjf2, bo.getMcgsJjf2());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf3()), McgsJjf2z::getMcgsJjf3, bo.getMcgsJjf3());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf4()), McgsJjf2z::getMcgsJjf4, bo.getMcgsJjf4());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf5()), McgsJjf2z::getMcgsJjf5, bo.getMcgsJjf5());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf6()), McgsJjf2z::getMcgsJjf6, bo.getMcgsJjf6());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf7()), McgsJjf2z::getMcgsJjf7, bo.getMcgsJjf7());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf8()), McgsJjf2z::getMcgsJjf8, bo.getMcgsJjf8());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf9()), McgsJjf2z::getMcgsJjf9, bo.getMcgsJjf9());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf10()), McgsJjf2z::getMcgsJjf10, bo.getMcgsJjf10());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsBy1()), McgsJjf2z::getMcgsBy1, bo.getMcgsBy1());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsZsch()), McgsJjf2z::getMcgsZsch, bo.getMcgsZsch());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), McgsJjf2z::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), McgsJjf2z::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, McgsJjf2z::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), McgsJjf2z::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, McgsJjf2z::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增制动阀试验台_紧急阀2组_MCGS
     */
    @Override
    public Boolean insertByBo(McgsJjf2zBo bo) {
        McgsJjf2z add = BeanUtil.toBean(bo, McgsJjf2z.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改制动阀试验台_紧急阀2组_MCGS
     */
    @Override
    public Boolean updateByBo(McgsJjf2zBo bo) {
        McgsJjf2z update = BeanUtil.toBean(bo, McgsJjf2z.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(McgsJjf2z entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除制动阀试验台_紧急阀2组_MCGS
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String getMaxMcgsTime(){
        return baseMapper.getMaxMcgsTime();
    }
}
